/*
 * Salas.java
 *
 * Created on 6 de Novembro de 2007, 17:39
 */

package cadastro;

import bd.Database;
import classes.Util;
import classes.sessao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;



/**
 *
 * @author  kurumin
 */
public class cadSessao extends javax.swing.JFrame
{
    /** Creates new form Salas */
    public cadSessao(Database meuBanco)
    {
       initComponents();
       this.bd = meuBanco;
       secao = new sessao(bd);
       prenchecombo();
       
    }
    
    
   
    // <editor-fold defaultstate="collapsed" desc=" C�digo Gerado ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        javax.swing.JLabel jLabel3;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        rbHorario = new javax.swing.JRadioButton();
        rbFilme = new javax.swing.JRadioButton();
        rbData = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbSessao = new javax.swing.JTable();
        jtfPesquisa = new javax.swing.JTextField();
        botaopesquisar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        botaoeditar = new javax.swing.JButton();
        jtf_data = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbFilme = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jcbSala = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtfValor = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        botaocadastrar = new javax.swing.JButton();
        botaonovo = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        try
        {
            formatodata = new MaskFormatter("##/##/####");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"N�o foi possivel setar a Mascara");
        }
        jtfdata = new JFormattedTextField(formatodata);
        try
        {
            formatohora = new MaskFormatter("##:##:##");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"N�o foi possivel setar a Mascara");
        }
        jtfhora = new JFormattedTextField(formatohora);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Sessao");
        setResizable(false);
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisa"));
        buttonGroup1.add(rbHorario);
        rbHorario.setFont(new java.awt.Font("Dialog", 0, 12));
        rbHorario.setSelected(true);
        rbHorario.setText("Hor\u00e1rio");
        rbHorario.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbHorario.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(rbFilme);
        rbFilme.setFont(new java.awt.Font("Dialog", 0, 12));
        rbFilme.setText("Filme");
        rbFilme.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbFilme.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(rbData);
        rbData.setFont(new java.awt.Font("Dialog", 0, 12));
        rbData.setText("Data");
        rbData.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbData.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rbHorario)
                .addGap(60, 60, 60)
                .addComponent(rbFilme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                .addComponent(rbData)
                .addGap(22, 22, 22))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(rbData)
                .addComponent(rbHorario)
                .addComponent(rbFilme))
        );

        jtbSessao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {

            }
        ));
        jScrollPane1.setViewportView(jtbSessao);

        botaopesquisar.setText("Pesquisar");
        botaopesquisar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botaopesquisarActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel6.setText("Pesquisar");

        botaoeditar.setText("Editar");
        botaoeditar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botaoeditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addContainerGap(286, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botaoeditar))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jtfPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                            .addComponent(botaopesquisar))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaopesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoeditar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("Consulta", jPanel1);

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel1.setText("Hor\u00e1rio");

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel2.setText(" Data");

        jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel3.setText("Filme");

        jLabel4.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel4.setText("Sala");

        jLabel5.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel5.setText("Valor");

        jPanel3.setBackground(new java.awt.Color(204, 204, 204));
        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel3.setForeground(new java.awt.Color(204, 204, 204));
        botaocadastrar.setMnemonic('G');
        botaocadastrar.setText("Gravar");
        botaocadastrar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botaocadastrarActionPerformed(evt);
            }
        });

        botaonovo.setMnemonic('N');
        botaonovo.setText("Novo");
        botaonovo.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botaonovoActionPerformed(evt);
            }
        });

        jButton1.setMnemonic('D');
        jButton1.setText("Deletar");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(botaonovo, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaocadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaocadastrar)
                    .addComponent(botaonovo)
                    .addComponent(jButton1))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jtf_dataLayout = new javax.swing.GroupLayout(jtf_data);
        jtf_data.setLayout(jtf_dataLayout);
        jtf_dataLayout.setHorizontalGroup(
            jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jtf_dataLayout.createSequentialGroup()
                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jtf_dataLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addGroup(jtf_dataLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addGroup(jtf_dataLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jtf_dataLayout.createSequentialGroup()
                                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jtfhora, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jtf_dataLayout.createSequentialGroup()
                                        .addGap(4, 4, 4)
                                        .addComponent(jLabel2))
                                    .addGroup(jtf_dataLayout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtfdata, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfValor, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jcbSala, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jcbFilme, javax.swing.GroupLayout.Alignment.LEADING, 0, 319, Short.MAX_VALUE)))
                        .addGap(23, 23, 23))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jtf_dataLayout.setVerticalGroup(
            jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jtf_dataLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jtf_dataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfhora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfdata, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfValor, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbFilme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jTabbedPane1.addTab("Cadastro", jtf_data);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-368)/2, (screenSize.height-333)/2, 368, 333);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
       String mensagem;
       if(secao.deleta())
           mensagem = "Registro deletado com sucesso";
       else
          mensagem = "Erro ao tentar deletar o registro";
       JOptionPane.showMessageDialog(null,mensagem);
       limpa();
       grava = true;
    }//GEN-LAST:event_jButton1ActionPerformed

    private void botaoeditarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botaoeditarActionPerformed
    {//GEN-HEADEREND:event_botaoeditarActionPerformed
// TODO adicione seu c�digo de manipula��o aqui:
        editar();
    }//GEN-LAST:event_botaoeditarActionPerformed

    private void botaonovoActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botaonovoActionPerformed
    {//GEN-HEADEREND:event_botaonovoActionPerformed
// TODO adicione seu c�digo de manipula��o aqui:
        limpa();
        grava = true;
    }//GEN-LAST:event_botaonovoActionPerformed

    private void botaopesquisarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botaopesquisarActionPerformed
    {//GEN-HEADEREND:event_botaopesquisarActionPerformed
// TODO adicione seu c�digo de manipula��o aqui:
        pesquisa();
    }//GEN-LAST:event_botaopesquisarActionPerformed

    private void botaocadastrarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botaocadastrarActionPerformed
    {//GEN-HEADEREND:event_botaocadastrarActionPerformed
// TODO adicione seu c�digo de manipula��o aqui:
       gravar();
    }//GEN-LAST:event_botaocadastrarActionPerformed
    
    private void prenchecombo()
    {
        ResultSet rs = bd.executeQueryRs("Select filme.id_filme, filme.titulo From filme");
        try {
            while (rs.next()){
                filme.add(rs.getString("titulo"));
                id_filme.add(rs.getString("id_filme"));
            }
            for(int i=0;i<filme.size();i++){
                jcbFilme.addItem(filme.get(i));
            }
        }    
        catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        rs = bd.executeQueryRs("Select sala.id_sala, sala.nome From sala");
        try {
            while (rs.next()){
                id_sala.add(rs.getString("id_sala"));
                sala.add(rs.getString("nome"));
            }
            for(int i=0;i<sala.size();i++){
                jcbSala.addItem(sala.get(i));
            }
        }    
        catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    private void pesquisa()
    {
        String[] colunas = new String[]{"C�digo","Hor�rio","Filme","Data",};  
        String[][] dados = new String[][]{{}};
        sessao = new DefaultTableModel(dados,colunas);
        String sql;
        ResultSet rs;
        String parametro;
        if(rbHorario.isSelected())
            parametro = "horario";
        else if(rbData.isSelected())
            parametro = "data";
        else
            parametro = "id_filme";
        try
        {
            sql= "SELECT sessao.id_sessao,to_char(sessao.data,'dd/MM/yyyy')as data,"
                +"to_char(sessao.horario,'hh:mm:ss')as horario,sessao.valor,sessao.id_filme,filme.titulo "
                +"FROM sessao JOIN filme ON sessao.id_filme = filme.id_filme AND LOWER(sessao."+parametro+") LIKE LOWER('%"+jtfPesquisa.getText()+"%')";
            rs = bd.executeQueryRs(sql);
            if(bd.Error){
                JOptionPane.showMessageDialog(null,bd.ErrorMessage);
                return;
            }
            String sessoes[] = new String[4];
            int aux = 0;
            while( rs.next())
            {
                sessoes[0] = rs.getString("id_sessao");
                sessoes[1] = rs.getString("titulo");
                sessoes[2] = rs.getString("data");
                sessoes[3] = rs.getString("horario");
                sessao.addRow(sessoes);     
                if (aux == 0){
                    sessao.removeRow(0);
                }
                aux++;
            }
            jtbSessao.setModel(sessao);
            
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null,e);
        }
    }
    private void limpa()
    {
        jtfhora.setText("");
        jtfdata.setText("");
        jtfValor.setText("");
        jtfPesquisa.setText("");
        sessao = (DefaultTableModel) jtbSessao.getModel(); 
        sessao.setNumRows(0);
        jcbFilme.setSelectedIndex(1);
        jcbSala.setSelectedIndex(0);        
    }
    private void gravar()
    {
        String mensagem;
        int f = Util.posicao(jcbFilme.getSelectedItem().toString(),filme);
        int s = Util.posicao(jcbSala.getSelectedItem().toString(),sala);
        secao.sethorario(jtfhora.getText());
        secao.setdata(jtfdata.getText());
        secao.setvalor(jtfValor.getText());
        secao.setidsala(id_sala.get(s));
        secao.setidfilme(id_filme.get(f));
        if(secao.verifica()){
           if(grava){
             if(secao.gravar())
               mensagem = "Registro salvo com sucesso";
             else
               mensagem = "Erro ao tentar gravar o registro";
           }  
           else{
               if(secao.atualizar())
                   mensagem = "Registro salvo com sucesso";
               else
                   mensagem = "Erro ao tentar gravar o registro";
           }
           JOptionPane.showMessageDialog(null,mensagem);
        }
        else{
          JOptionPane.showMessageDialog(null,"N�o foi possivel gravar com esses dados!!!");
        }
    }
    private void editar()
    {
        int  l = jtbSessao.getSelectedRow();
        String id = jtbSessao.getValueAt(l,0).toString();
        Util aux = new Util();
               
        secao.setid(id);
        secao.obter();
        limpa();
        
        jtfdata.setText(secao.getdata());
        jtfhora.setText(secao.gethorario());
        jtfValor.setText(secao.getvalor());
        jcbFilme.setSelectedIndex(aux.posicao(secao.getidfilme(),id_filme));
        jTabbedPane1.setSelectedIndex(1);
        grava=false; 
    }
    
    // Declara��o de vari�veis - n�o modifique//GEN-BEGIN:variables
    private javax.swing.JButton botaocadastrar;
    private javax.swing.JButton botaoeditar;
    private javax.swing.JButton botaonovo;
    private javax.swing.JButton botaopesquisar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JComboBox jcbFilme;
    private javax.swing.JComboBox jcbSala;
    private javax.swing.JTable jtbSessao;
    private javax.swing.JTextField jtfPesquisa;
    private javax.swing.JTextField jtfValor;
    private javax.swing.JPanel jtf_data;
    private javax.swing.JFormattedTextField jtfdata;
    private javax.swing.JFormattedTextField jtfhora;
    private javax.swing.JRadioButton rbData;
    private javax.swing.JRadioButton rbFilme;
    private javax.swing.JRadioButton rbHorario;
    // Fim da declara��o de vari�veis//GEN-END:variables
    private ArrayList <String> filme = new ArrayList <String>();
    private ArrayList <String> id_filme = new ArrayList <String>();
    private ArrayList <String> sala = new ArrayList <String>();
    private ArrayList <String> id_sala = new ArrayList <String>();
    private Database bd;
    DefaultTableModel sessao;
    private MaskFormatter formatocpf,formatohora,formatodata;
    private sessao secao;
    private boolean grava = true;
}
